<template>
  <div class="main">
    <div class="search">
      <el-form :inline="true" :model="search">
<!--        <el-form-item>-->
<!--          <el-button type="success" size="small" @click="form = { status: true }; dialogShow = true">添加</el-button>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="用户名">-->
<!--          <el-input v-model="search.username" size="small" placeholder="用户名" clearable></el-input>-->
<!--        </el-form-item>-->
        <el-form-item label="评论内容">
          <el-input v-model="search.message" size="small" placeholder="评论内容" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="info" size="small" @click="searchClick">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="list">
      <el-table
          :data="data"
          stripe
          style="width: 100%">
        <el-table-column label="用户昵称">
          <template slot-scope="props">
            {{ props.row.user ? props.row.user.nickname : '' }}
          </template>
        </el-table-column>
        <el-table-column label="用户姓名">
          <template slot-scope="props">
            {{ props.row.user ? props.row.user.name : '' }}
          </template>
        </el-table-column>
        <el-table-column prop="message" label="评论内容" show-overflow-tooltip></el-table-column>
        <el-table-column prop="createTime" label="评论时间"></el-table-column>
        <el-table-column label="操作" width="170px">
          <template slot-scope="scope">
<!--            <el-button type="primary" @click="form = {...scope.row}; dialogShow = true">编辑</el-button>-->
            <el-button type="danger" @click="del(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          @size-change="sizeChange"
          @current-change="currentChange"
          :current-page="pageNo"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
    <el-dialog
        :title="form.id ? '修改' : '添加'"
        :visible.sync="dialogShow"
        width="600px"
        @close="dialogClose"
    >
      <el-form ref="form" :model="form" label-width="100px" :rules="rules">
        <el-form-item prop="username" label="用户名">
          <el-input type="text" placeholder="请输入用户名" v-model="form.username"/>
        </el-form-item>
        <el-form-item prop="password" label="密码">
          <el-input type="password" placeholder="请输入密码(默认为123456)" v-model="form.password"/>
        </el-form-item>
        <el-form-item prop="phone" label="联系电话">
          <el-input type="text" placeholder="请输入联系电话" v-model="form.phone"/>
        </el-form-item>
        <el-form-item prop="status" label="状态">
          <el-switch
              v-model="form.status"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-text="启用"
              inactive-text="禁用"
              :active-value="true"
              :inactive-value="false">
          </el-switch>
        </el-form-item>
        <el-form-item prop="description" label="备注">
          <el-input type="textarea" placeholder="请输入备注" v-model="form.description"/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submit">保存</el-button>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogShow = false">关闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "comment",
  data() {
    return {
      search: {},
      dialogShow: false,
      form: {
        status: true
      },
      data: [],
      pageNo: 1,
      pageSize: 10,
      total: 0,
      rules: {
        username: [
          {
            required: true,
            type: 'string',
            message: '用户不能为空',
            trigger: 'blur'
          },
          {
            min: 3,
            max: 30,
            message: '用户名长度在3-30位之间',
            trigger: 'blur'
          },
          {
            pattern: /^[0-9A-Za-z_]{3,30}$/,
            message: '用户名只能包含数字、字母、下划线',
            trigger: 'blur'
          },
          {
            validator: (rule, val, cb) => {
              let url = `comment/repeat?username=${val}`
              if (this.form.id) url += `&id=${this.form.id}`
              this.$http.get(url).then(res => {
                if (res.code === 200) {
                  if (res.data) {
                    return cb(new Error('用户名已存在！'))
                  }
                  cb()
                }
              })
            },
            trigger: 'blur'
          }
        ],
        password: [
          {
            required: false,
            type: 'string',
            trigger: 'blur'
          },
          {
            min: 6,
            max: 30,
            message: '密码长度在6-30位之间',
            trigger: 'blur'
          },
          {
            pattern: /^[a-zA-Z0-9-*/+.~!@#$%^&*()]{6,30}$/,
            message: '密码只能包含数字、字母、特殊字符',
            trigger: 'blur'
          }
        ],
        phone: [
          {
            required: true,
            type: 'string',
            message: '电话不能为空',
            trigger: 'blur'
          },
          {
            pattern: /^((13\d|14[57]|15[^4,\D]|17[678]|18\d)\d{8}|170[059]\d{7})$/,
            message: '电话号码格式不正确',
            trigger: 'blur'
          }
        ],
        description: [
          {
            min: 0,
            max: 800,
            message: '备注长度在3-800位之间',
            trigger: 'blur'
          }
        ]
      },
    }
  },
  mounted() {
    this.getData()
  },
  methods: {
    getData() {
      this.$http.post(`comment/page?pageNo=${this.pageNo}&pageSize=${this.pageSize}`, this.search).then(res => {
        if (res.code === 200) {
          this.pageSize = res.data.size
          this.pageNo = res.data.current
          this.total = res.data.total
          this.data = res.data.records
        }
      })
    },
    sizeChange(val) {
      this.pageNo = 1;
      this.pageSize = val;
      this.getData();
    },
    currentChange(val) {
      this.pageNo = val;
      this.getData();
    },
    searchClick() {
      this.pageNo = 1;
      this.getData()
    },
    dialogClose() {
      this.form = { status: true }
      this.$refs['form'].clearValidate()
    },
    submit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.id) {
            this.$http.put(`comment`, this.form).then(res => {
              if(res.code === 200) {
                this.$Message.success(res.msg)
                this.getData()
                this.dialogShow = false
              }
            })
          } else {
            this.$http.post(`comment`, this.form).then(res => {
              if(res.code === 200) {
                this.$Message.success(res.msg)
                this.getData()
                this.dialogShow = false
              }
            })
          }
        }
      });
    },
    del(id) {
      this.$confirm('是否确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http.delete(`comment/${id}`).then(res => {
          if (res.code === 200) {
            this.getData()
            this.$Message.success(res.msg)
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    }
  }
}
</script>

<style scoped>

</style>
